package com.aos.erp.manager.util;

import com.aos.erp.common.config.SpringContextUtils;
import com.aos.erp.common.exception.BizException;

import com.aos.erp.manager.model.sys.dto.UserDto;
import com.aos.erp.manager.service.sys.UserService;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description: 用户工具类
 * @author: tanzhiyu
 * @create: 2018-11-08 16:10
 **/
public class UserUtil {
    private static UserService userService = SpringContextUtils.getBean(UserService.class);
   // private static IndexQueryService schoolService=SpringContextUtils.getBean(IndexQueryService.class);
   public static UserDto getCurrentUserInfo(String userId) throws Exception {
       if(StringUtils.isEmpty(userId)){
           throw new BizException("请重新登录");
       }
       long optUserId = 0;
       try {
           optUserId = Long.parseLong(userId);
       } finally {
           if(optUserId == 0) {
               throw new BizException("无效的用户，请重新登录");
           }
       }

       UserDto userDto = userService.findById(optUserId);
       if(userDto == null) {
           throw new BizException("无效的用户，请重新登录");
       }
       return userDto;
   }

    /**
     * 获取用户部门组织code
     * @param id    用户id
     * @return
     * @throws Exception
     */
    public static String getUserDepCode(String id) throws Exception {
        if(StringUtils.isEmpty(id) || "null".equals(id)){
            throw new BizException("请重新登录");
        }
       UserDto userDto = userService.findById(Long.valueOf(id));
       if(userDto==null){
           throw new BizException("用户id错误");
       }
       String depCode=userDto.getDepCode();
       return depCode;
    }

    /**
     * 获取用户关联的驾校id
     * @param id    用户id
     * @return
     * @throws Exception
     */
   /* public static String getUserSchoolId(String id) throws Exception {
        if(StringUtils.isBlank(id)){
            throw new BizException("请重新登录");
        }
        UserDto userDto = userService.findById(Long.valueOf(id));
        if(userDto==null){
            throw new BizException("用户id错误");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("departmentCode",userDto.getDepCode());
        List<ApplySchool> list = schoolService.queryListVo(map);
        if(list==null || list.size()==0){
            return null;
        }else{
            return  list.get(0).getId().toString();
        }
    }*/
}
